package com.xgkj.diyiketang.weidget;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.util.PalLog;


public class MyLoadDataListView extends ListView implements OnScrollListener{
	private static final String TAG = "MyLoadDataListView";
	private BaseAdapter mAdapter;
	private int mLastItemIndex;
	private boolean mCanLoadMore = false;
	LoadDataListener mLoadDataListener;
	private View mFootView;
	private ProgressBar mFootProgress;
	private TextView mFootText;
	private Context mContext;
	
	public MyLoadDataListView(Context context) {
		super(context);
		mContext = context;
		init();
	}

	public MyLoadDataListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init();
	}

	public MyLoadDataListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}
	
	public void init(){
		mFootView = LayoutInflater.from(mContext).inflate(R.layout.foot_moreloading, null);
		mFootProgress = (ProgressBar) mFootView.findViewById(R.id.foot_progress);
		mFootText = (TextView) mFootView.findViewById(R.id.foot_text);
		mFootProgress.setVisibility(View.GONE);
		setOnScrollListener(this);
		addFooterView(mFootView);
	}
	
	public void setAdapter(BaseAdapter adapter) {
		super.setAdapter(adapter);
		mAdapter = adapter;
		removeFooterView(mFootView);
	}
	
	public void enableLoadMore(){
		mCanLoadMore = true;
	}
	
	public void disableLoadMore(){
		mCanLoadMore = false;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
//		PalLog.e(TAG, "onScroll");
		if(mAdapter != null){
			if (mLastItemIndex >= (mAdapter.getCount() - 1)
					&& scrollState == OnScrollListener.SCROLL_STATE_IDLE
					&& mCanLoadMore) {
				
				if(mLoadDataListener != null){
					if(getFooterViewsCount() == 0){
						addFooterView(mFootView);
					}
					setFootProgressVisible(true);
					mLoadDataListener.onLoadMore();
				}
			}
		}
	}
	

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		mLastItemIndex = firstVisibleItem +visibleItemCount -1;
//		PalLog.e(TAG, mLastItemIndex +"");
	}
	
	public interface LoadDataListener{
		public void onLoadMore();
	}
	
	public void setOnLoadDataListener(LoadDataListener listener){
		mLoadDataListener = listener;
	}
	
	public void setNoNextPage(){
		if(getFooterViewsCount() == 0){
			addFooterView(mFootView);
		}
		mFootView.setVisibility(View.VISIBLE);
		mFootProgress.setVisibility(View.GONE);
		mFootText.setText(R.string.no_next_page);
        mCanLoadMore = false;
	}
	
	public void setFootViewHidden(){
		removeFooterView(mFootView);
	}
	
	public void setFootProgressVisible(boolean v) {
		if(getFooterViewsCount() == 0){
			addFooterView(mFootView);
		}
        mFootText.setVisibility(View.VISIBLE);
		if (v) {
			mFootProgress.setVisibility(View.VISIBLE);
			mFootText.setText(R.string.get_more);
		} else {
			mFootProgress.setVisibility(View.GONE);
			mFootText.setText(R.string.get_more_fail);
			mFootView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
                    if (mFootText.getVisibility()==View.VISIBLE&&mFootText.getText().equals(mContext.getString(R.string.get_more_fail))) {
                    	if(mLoadDataListener != null){
        					setFootProgressVisible(true);
        					mLoadDataListener.onLoadMore();
        				}
					}
				}
			});
		}
	}
	
	

	

}
